/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.sessionbean;

import cms.dataaccess.DAOFactory;
import dto.Confitemorder;
import dto.Participant;
import dto.Payment;
import dto.PaymentId;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author TauPo
 */
@Stateless(mappedName="PaymentMgmt")
public class PaymentMgmt implements PaymentMgmtRemote {

    @Override
   public List<Participant> getConferencesParticipant(String conferenceName) {
        try {
      
         return DAOFactory.getInstance().getParticipantDAO().
                 findParticipantByConferenceName(conferenceName);
       }
         catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }
    @Override
   public Participant getConferencesParticipant(String conferenceName,String userName) {
        try {
      
         return DAOFactory.getInstance().getParticipantDAO().findParticipant(conferenceName, userName);
       }
         catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }
    @Override
    public List<Participant> getAllConferencesParticipant() {
          try {
      
         return DAOFactory.getInstance().getParticipantDAO().findAllParticipant();
              
       }
         catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }

    @Override
    public List<Participant> getConferencesParticipantByUserName(String userName) {
       try {
      
            return DAOFactory.getInstance().getParticipantDAO().findParticipantByUserName(userName);
              
       }
         catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }

    @Override
    public List<Confitemorder> getAllItemOrders() {
        try{
          return  DAOFactory.getInstance().getConferenceItemOrderDAO().findAllConfitemorder();
        }catch(Exception e ){
            System.err.println("get All Item Order fail==>"+e.getMessage());
            return null;
        }
    
    }

    @Override
    public List<Confitemorder> getItemOrders(String conferenceName, String itemName) {
       try{
          String custQuery = "conferenceName = '"+conferenceName+"' and itemName='"+itemName+"'";
          return  DAOFactory.getInstance().getConferenceItemOrderDAO().findByCustomizeQuery(custQuery);
        }catch(Exception e ){
            System.err.println("get  Item Order fail conferenceName:"+conferenceName+ " itemName:"
                    +itemName+ " ==>"+e.getMessage());
            return null;
        }
    }

    @Override
    public List<Confitemorder> getItemOrdersByConferenceName(String conferenceName) {
          try{
          return  DAOFactory.getInstance().
                  getConferenceItemOrderDAO().
                  findConfitemorderByConferenceName(conferenceName);
        }catch(Exception e ){
            System.err.println("get  Item Order fail conferenceName:"+conferenceName 
                    + " ==>"+e.getMessage());
           return null;
        }
    
    }

    @Override
    public List<Confitemorder> getItemOrdersByUserName(String userName) {
         try{
          return  DAOFactory.getInstance().
                  getConferenceItemOrderDAO()
                  .findConfitemorderByOrderUser(userName);
                 
        }catch(Exception e ){
            System.err.println("get All Item Order fail userName:"
                    +userName+"==>"+e.getMessage());
            return null;
        }
    
    }

    @Override
    public Confitemorder getItemorder(String conferenceName, String itemName, String userName) {
         try{
          return  DAOFactory.getInstance().
                  getConferenceItemOrderDAO()
                 .findConfitemorder(conferenceName, itemName, userName);
                 
        }catch(Exception e ){
            System.err.println("get All Item Order fail conferenceName,itemName, userName("
                    + conferenceName+","+itemName+","+ userName+")==>"+e.getMessage());
            return null;
        }
    }

   

    @Override
    public boolean updatePayment(Payment payment) {
        try{
            DAOFactory.getInstance().getPaymentDAO().updatePayment(payment);
            return true;
        }catch(Exception e ){
            System.err.println("update the  Payment fail ==>"+e.getMessage());
           return true;
        }
    }
    @Override
    public boolean deletePayment(String userName, String payName, int payType) {
            try{
                Payment delPay = this.getPayment(userName, payName, payType);
            DAOFactory.getInstance().getPaymentDAO().deletePayment(delPay);
            return true;
        }catch(Exception e ){
            System.err.println("delete the  Payment fail ==>"+e.getMessage());
           return false;
        }
    }
    
    @Override
    public List<Payment> getAllPayments() {
        try{
           return DAOFactory.getInstance().getPaymentDAO().findAllPayment();
           
        }catch(Exception e ){
            System.err.println("get All payment fail ==>"+e.getMessage());
           return null;
        }
    }

    @Override
    public List<Payment> getAllPayments(String userName) {
       try{
           return DAOFactory.getInstance().getPaymentDAO().findPayment(userName);
           
        }catch(Exception e ){
            System.err.println("get All payment fail for userName"+userName+"==>"+e.getMessage());
           return null;
        }
    }

    @Override
    public List<Payment> getAllConferencePayments(String conferenceName) {
           try{
           ArrayList<Payment> allResList = new  ArrayList<Payment>();
           List<Payment> consPayList=this.getConferencePaymentsOnly(conferenceName);
           
           List<Confitemorder> itemsList =this.getItemOrdersByConferenceName(conferenceName);
           
           
           for(int i =0;i<consPayList.size();i++){
               allResList.add(consPayList.get(i));
           }
           
           for(int i =0; i <itemsList.size();i++ ){
               List<Payment> itemsPayList =this.getAllItemPayments(itemsList.get(i).getId().getItemName());
               for(int j=0;j<itemsPayList.size();j++){
                    allResList.add(itemsPayList.get(j));
               }
           }
           return allResList;
                   
           
        }catch(Exception e ){
            System.err.println("get All Conference Payments by conference name"+conferenceName+"==>"+e.getMessage());
           return null;
        }
    }
    
    @Override
    public List<Payment> getConferencePaymentsOnly(String conferenceName) {
            try{
          return  DAOFactory.getInstance().getPaymentDAO()
                   .findPaymentPayName(conferenceName, 1);
          } catch(Exception e ){
            System.err.println("get the conference payments only fail by conferenceName:"+conferenceName+"==>"+e.getMessage());
           return null;
        }
    }

    @Override
    public List<Payment> getAllItemPayments(String itemName) {
          try{
          return  DAOFactory.getInstance().getPaymentDAO()
                   .findPaymentPayName(itemName, 2);
          } catch(Exception e ){
            System.err.println("get All conference item payment fail by item name:"+itemName+"==>"+e.getMessage());
           return null;
        }
          
    }
   
    @Override
    public Payment getPayment(String userName, String payName, int payType) {
        try{
          return  DAOFactory.getInstance().getPaymentDAO()
                  .findPayment(userName, payName, payType);
          } catch(Exception e ){
            System.err.println("get the payment fail for user name["+userName+"] payName["
            +payName+"] payType["+payType+"] ==>"+e.getMessage());
           return null;
        }
    }

    @Override
    public boolean doConferencePayment(String userName, double payAmt, String conferenceName) {
       try{
            Participant conferenceOrder = this.getConferencesParticipant(conferenceName, userName);

            Payment confPay =this.getPayment(userName, conferenceName, 1);
            if(confPay==null){
                PaymentId payID= new PaymentId(userName,conferenceName,1);
                confPay =  new Payment(payID);
            }


            DateFormat formatter =new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
            java.util.Date dt = new java.util.Date();
            String currentTime = formatter.format(dt);
          
           
            Date paydate =  formatter.parse(currentTime);
            Date apprDate = formatter.parse(currentTime);
            if(conferenceOrder.getPrice() <= payAmt){
                 conferenceOrder.setStatus(2);
               //  conferenceOrder.setPrice(conferenceOrder.getPrice()-payAmt);
                 confPay.setAmount(payAmt);
                 confPay.setPayStatus(2);
                 confPay.setPayDate(paydate);
                 confPay.setApprover("sysAuto");
                 confPay.setApproveDate(apprDate);
            }
            else
            {
                conferenceOrder.setStatus(1);
              //  conferenceOrder.setPrice(conferenceOrder.getPrice()-payAmt);
                confPay.setAmount(payAmt);
                confPay.setPayStatus(1);
                confPay.setPayDate(paydate);

            }

           this.updatePayment(confPay);
           DAOFactory.getInstance().getParticipantDAO().updateParticipant(conferenceOrder); 

           return true;
       }
       catch(Exception e){
           System.err.println("pay the conference fee fail:"+e.getMessage());
           return false;
       }
    }

    @Override
    public boolean doItemOrderPayment(String userName,double itemAmt, String conferenceName, String itemName) {
        try{
            Confitemorder itemOrder = this.getItemorder(conferenceName, itemName, userName);

            Payment itemPay =this.getPayment(userName, itemName, 2);
            if(itemPay==null){
                PaymentId payID= new PaymentId(userName,itemName,2);
                itemPay =  new Payment(payID);
            }



             DateFormat formatter =new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
            java.util.Date dt = new java.util.Date();
            String currentTime = formatter.format(dt);
          
           
            Date paydate =  formatter.parse(currentTime);
            Date apprDate = formatter.parse(currentTime);
            if(itemOrder.getPrice() >= itemAmt){
                 itemOrder.setStatus(2);
             //    itemOrder.setPrice(itemOrder.getPrice()-itemAmt);
                 itemPay.setAmount(itemAmt);
                 itemPay.setPayStatus(2);
                 itemPay.setPayDate(paydate);
                 itemPay.setApprover("sysAuto");
                 itemPay.setApproveDate(apprDate);
            }
            else
            {
                itemOrder.setStatus(1);
              //  itemOrder.setPrice(itemOrder.getPrice()-itemAmt);
                itemPay.setAmount(itemAmt);
                itemPay.setPayStatus(1);
                itemPay.setPayDate(paydate);

            }

           this.updatePayment(itemPay);
           DAOFactory.getInstance().getConferenceItemOrderDAO().updateConfitemorder(itemOrder);
           return true;
       }
       catch(Exception e){
           System.err.println("pay the conference fee fail:"+e.getMessage());
           return false;
       }
    }

    
    @Override
     public boolean approveConferencePaymentOnly(String userName,String conferenceName,int payStatus,String approverName) {
       try{
            Payment conferencePay =this.getPayment(userName, conferenceName, 1);
            if(conferencePay==null){
               PaymentId payID = new PaymentId(userName, conferenceName,1);
               conferencePay = new Payment(payID);
          }

            Participant conOrder =this.getConferencesParticipant(conferenceName, userName);
            if(conOrder==null){
                return false;
            }

             DateFormat formatter =new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
            java.util.Date dt = new java.util.Date();
            String currentTime = formatter.format(dt);
          
              Date apprDate = formatter.parse(currentTime);
              
              
            conferencePay.setAmount(conOrder.getPrice());
            conferencePay.setPayStatus(payStatus);  
            conferencePay.setApproveDate(apprDate);
            conferencePay.setApprover(approverName);

            conOrder.setStatus(payStatus);
            this.updatePayment(conferencePay);
            DAOFactory.getInstance().getParticipantDAO().updateParticipant(conOrder);
            return true;
       }catch(Exception e){
           System.err.println("approve conference payment fail:"+e.getMessage());
           return false;
       }

     }

    @Override
    public boolean approveItemPayment(String userName, String conferenceName, String itemName, int payStatus, String approverName) {
       try{
            Payment itemPay =this.getPayment(userName, itemName, 2);
            if(itemPay==null){
               PaymentId payID = new PaymentId(userName, itemName,2);
               itemPay = new Payment(payID);
            }

            Confitemorder itemOrder =this.getItemorder(conferenceName, itemName, userName);
            if(itemOrder==null){
                return false;
            }
           DateFormat formatter =new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
            java.util.Date dt = new java.util.Date();
            String currentTime = formatter.format(dt);
          
           
            Date apprDate = formatter.parse(currentTime);
            itemPay.setAmount(itemOrder.getPrice());  
            itemPay.setPayStatus(payStatus);  
            itemPay.setApproveDate(apprDate);
            itemPay.setApprover(approverName);

            itemOrder.setStatus(payStatus);
            this.updatePayment(itemPay);
            DAOFactory.getInstance().getConferenceItemOrderDAO().updateConfitemorder(itemOrder);
            return true;
       }catch(Exception e){
           System.err.println("approve conference payment fail:"+e.getMessage());
           return false;
       }

     
    }

    @Override
    public List<Confitemorder> getItemOrdersByConfAndUser(String conferenceName, String userName) {
         try{
          String custQuery = "conferenceName = '"+conferenceName+"' and ItemOrderBy='"+userName+"'";
          return  DAOFactory.getInstance().getConferenceItemOrderDAO().findByCustomizeQuery(custQuery);
        }catch(Exception e ){
            System.err.println("get  Item Order fail conferenceName:"+conferenceName+ " ItemOrderBy:"
                    +userName+ " ==>"+e.getMessage());
            return null;
        }
    }
     

    





   
}
